https://leetcode.com/problems/n-th-tribonacci-number/
Tribonacci
的規則如下:
T0 = 0
T1 = 1
T2 = 1
Tn + 3 = Tn + Tn+1 + Tn+2
請回傳Tn的結果
其實就是費氏數列的改版,原本的前兩項相加變成前三項相加
class Solution:
def tribonacci(self, n: int) -> int:
Tri = [0, 1, 1]
if n < 3:
return Tri[n]
else:
for i in range(3, n+1):
Tri.append(Tri[i-3] + Tri[i-2] + Tri[i-1])
return Tri[-1]
差點睡過頭趕不上